package com.bkhc.business.service;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.bkhc.business.entity.Customer;
import com.bkhc.business.entity.Group;
import com.bkhc.business.entity.Member;
import com.bkhc.business.entity.vo.Order;
import com.bkhc.business.entity.vo.OrderItem;
import com.bkhc.business.repository.CustomerRepository;
import com.bkhc.business.repository.GroupRepository;
import com.bkhc.business.repository.MemberRepository;
import com.bkhc.common.entity.User;
import com.bkhc.common.entity.UserRole;
import com.bkhc.common.repository.UmcRepository;
import com.bkhc.common.repository.UserRepository;
import com.bkhc.common.repository.UserRoleRepository;
import com.bkhc.common.util.CommonUtil;
import com.bkhc.common.util.response.PageUtil;
import com.bkhc.common.util.response.ResultCode;
import com.bkhc.common.util.response.ResultJson;
import com.bkhc.common.vo.UserBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
public class CustomerService {

    @Autowired
    private CustomerRepository repository;

    @Autowired
    private MemberRepository memberrepository;

    @Autowired
    private GroupRepository groupRepository;



    public ResultJson<?> queryCustomerByAdmin(Customer customer, int pageIndex, int pageSize) {
        UserBean user = CommonUtil.getCrruentUser();
        String userId = user.getUserId();
        Member member = memberrepository.findByUserId(userId);
        Sort.Order orders = new Sort.Order(Sort.Direction.DESC, "updateDate");
        PageRequest pageRequest = PageRequest.of(pageIndex-1, pageSize, Sort.by(orders));
        Page<Customer> page = null;
        if (member.isLeader()) {
            page = repository.findAll(customer, pageRequest);
        } else {
            customer.setInviteCode(member.getInviteCode());
            page = repository.findAll(customer, pageRequest);
        }

        PageUtil pageUtil = new PageUtil(page);
        ResultJson<PageUtil> resultJson = ResultJson.success();
        resultJson.setData(pageUtil);
        return  resultJson;
    }


    public ResultJson<?> assignmentToManage(Customer customer) {
        Customer entity = repository.findByUserId(customer.getUserId());
        entity.setGroupCode(customer.getGroupCode());
        repository.save(customer);
        ResultJson<Customer> resultJson = ResultJson.success();
        return resultJson;
    }

    public ResultJson<?> settingIdentity(Customer customer) {
        Customer entity = repository.findByUserId(customer.getUserId());
        entity.setIdentity(customer.getIdentity());
        repository.save(customer);
        ResultJson<Customer> resultJson = ResultJson.success();
        return resultJson;
    }

    public ResultJson<?> addCustomer(Customer customer) {
        if(StringUtils.isEmpty(customer.getInviteCode())){
            repository.save(customer);
            ResultJson<Customer> resultJson = ResultJson.success();
            return resultJson;
        }
        if(!StringUtils.isEmpty(customer.getInviteCode())){
            Member member = memberrepository.findByinviteCode(customer.getInviteCode());
            if(member==null){
                customer.setInviteCode("");
                customer.setGroupCode("");
            }else{
                customer.setGroupCode(member.getGroupCode());
                customer.setBusinessGroupMember(member.getUserId());
            }
        }
            customer.setIdentity("market");
            repository.save(customer);
        ResultJson<Customer> resultJson = ResultJson.success();
        return resultJson;

    }

    public ResultJson<Customer> queryCustomerByUserId(String userId) {
         Customer customer=repository.findByUserId(userId);
        ResultJson<Customer> resultJson = ResultJson.success();
        resultJson.setData(customer);
        return resultJson;
    }

    public ResultJson<?> assignmentToPerson(Customer customer) {
        customer.setUpdateDate(new Date());
        repository.save(customer);
        ResultJson<Customer> resultJson = ResultJson.success();
        return resultJson;
    }

}
